package cn.afactory.tygz.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.LifeServiceInfo;

/**
 *用户列表适配器
 */
public class LifeServiceListAdapter extends BaseAdapter {
    private List<LifeServiceInfo> mList = new ArrayList<LifeServiceInfo>();
    private Context mContext;

    public LifeServiceListAdapter(Context ctx, List<LifeServiceInfo> list) {
        mList = list;
        mContext = ctx;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LifeServiceInfo value = mList.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_life_service_list,null);

            holder.iv_life_service_image = (ImageView)convertView.findViewById(R.id.iv_life_service_image);
            holder.tv_life_service_distance = (TextView)convertView.findViewById(R.id.tv_life_service_distance);
            holder.tv_life_service_title = (TextView)convertView.findViewById(R.id.tv_life_service_title);
            holder.tv_life_service_price = (TextView)convertView.findViewById(R.id.tv_life_service_price);
            holder.tv_life_service_type = (TextView)convertView.findViewById(R.id.tv_life_service_type);
            holder.star_iv = (ImageView) convertView.findViewById(R.id.star_iv);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        if (value.iconPath != null) {
            BaseImgView.loadimg(holder.iv_life_service_image, value.iconPath, R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            holder.iv_life_service_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }


        if(value.name != null){
            holder.tv_life_service_title.setText(value.name);
        }

        holder.tv_life_service_type.setText(String.format(mContext.getString(R.string.label_unit_type),value.typeName));

        holder.tv_life_service_price.setText(String.format(mContext.getString(R.string.label_unit_price),value.price));

        holder.tv_life_service_distance.setText(String.format(mContext.getString(R.string.label_unit_distance),value.distance));

        int star = (int) value.score;
        switch (star){
            case 0:
                holder.star_iv.setImageResource(R.drawable.star_0);
                break;
            case 1:
                holder.star_iv.setImageResource(R.drawable.star_1);
                break;
            case 2:
                holder.star_iv.setImageResource(R.drawable.star_2);
                break;
            case 3:
                holder.star_iv.setImageResource(R.drawable.star_3);
                break;
            case 4:
                holder.star_iv.setImageResource(R.drawable.star_4);
                break;
            case 5:
                holder.star_iv.setImageResource(R.drawable.star_5);
                break;
        }

        return convertView;
    }


    public void setData(List<LifeServiceInfo> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    static class ViewHolder{
        public ImageView iv_life_service_image;
        public TextView tv_life_service_distance;
        public TextView tv_life_service_title;
        public TextView tv_life_service_price;
        public TextView tv_life_service_type;
        public ImageView star_iv;
    }
}